package cn.edu.imnu.vacate.users.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.imnu.common.controller.BaseController;
import cn.edu.imnu.common.util.ConstatFinalUtil;
import cn.edu.imnu.common.util.PageInfoUtil;
import cn.edu.imnu.vacate.leave.pojo.ALeave;
import cn.edu.imnu.vacate.leave.service.ILeaveService;
import cn.edu.imnu.vacate.users.pojo.ARole;
import cn.edu.imnu.vacate.users.pojo.AUser;
import cn.edu.imnu.vacate.users.service.IUsersService;

/**
 * 用户后台操作的controller
 * @author MCR
 *
 */
@Controller
@RequestMapping("/back/user/")
public class UserBackController extends BaseController
{
	@Resource
	private ILeaveService leaveService;
	@Resource
	private IUsersService usersService;
	/**
	 * 登陆后的首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("------main------");
		return "/back/user/main";
	}
	/**
	 * 请假列表页面
	 * @return
	 */
	@RequestMapping("/leaveList")
	public String leaveList(HttpServletRequest request,AUser user)
	{
		ConstatFinalUtil.LOGGER.info("-------leaveList------");
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		 /*处理搜索条件*/ 
		if(keyword == null)
		{
			keyword = ""; 
		}
		keyword = keyword.trim() ; 
		if(status == null)
		{
			status = ""; 
		}
		if(st == null)
		{
			st = ""; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		
		 /*查询条件*/ 
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		 /*分页*/ 
		PageInfoUtil pageInfoUtil = proccedPageInfo(request);
		
		 /*查询数据*/ 
		List<ALeave> leaveList = this.leaveService.findCondListLeaveService(pageInfoUtil, condMap);
		 /*将查询的数据放到四大作用域 */
		request.setAttribute("leaveList", leaveList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		 /*存储条件*/ 
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/user/leaveList";
	}
	
	/**
	 * 请假单添加页面
	 * @return
	 */
	@RequestMapping("/leaveInsert")
	public String leaveInsert(HttpServletRequest request,AUser user)
	{
		ConstatFinalUtil.LOGGER.info("-----leaveInsert-----");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", "1");
		List<ARole> roleList = this.usersService.findCondListRoleService(null, condMap );
		request.setAttribute("roleList", roleList);
		return "/back/user/leaveInsert";
	}
	
	/**
	 * 添加提交
	 * @return
	 */
	@RequestMapping("/leaveInsertSubmit")
	public String leaveInsertSubmit(HttpServletRequest request,AUser user)
	{
		
		return this.leaveInsert(request, user);
	}
	
	/**
	 * 退出登陆
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session,HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.LOGGER.info("------logout------");
		AUser user = (AUser) session.getAttribute("user");
		/*将sessio中的信息删除*/
		session.removeAttribute("user");
		session.removeAttribute("lastLoginTime");
		this.info = ConstatFinalUtil.INFO_JSON.getString("8");
		session.setAttribute("info", info);
		return "/back/login";
	}
}
